# -*- coding: utf-8 -*-

__Author__ = "NianJiu"
__QQ__ = 1670829014
__Email__ = "soliton.wang@gmail.com"

from AliDns import dns
from redirect import Mfpad
class Domain:
    def __init__(self, domain, ali_key, ali_secret, mf_token, mf_uuid):
        """
        :param domain: 主域名
        :param ali_key: 阿里KeyID
        :param ali_secret: 阿里Secret
        :param mf_token: 米发API token
        :param mf_uuid: 米发组织账户ID
        """
        self.domain = domain
        self.AliDNS = dns(Domain=domain, Ali_KeyId=ali_key, Ali_Secret=ali_secret)
        self.MFPAD = Mfpad(TOKEN=mf_token, UUID=mf_uuid)

    def add_domain_and_mfpad_dns(self, sub_domain: str, redirect_domain: str, port: str):
        # 例如： 阿里增加个A解析 test, IPV4地址:87.12.36.189
        # 然后： 米发新增一个URL隐性跳转源URL为www，跳转地址为：test.soliton.wang:8899
        # 最后： 通过米发新增的跳转，在阿里上面cname解析就可以 通过alog.soliton.wang 直接访问动态test.soliton.wang:8899
        """
        :param sub_domain: 动态解析的子域名, 例："test"
        :param redirect_domain: 源URL域名, 例："www"
        :param port: 内网服务端口
        :return: 输出成功或失败
        """

        add_ipv4 = self.AliDNS.ipv4_dns(sub_domain)
        print(add_ipv4)

        source_domain = redirect_domain + "." + self.domain
        redirect_url = sub_domain + "." + self.domain + ":" + port

        add_redirect = self.MFPAD.add_redirect(new_domain=source_domain, redirect_domain=redirect_url)
        print(add_redirect)

        domain_dns = self.MFPAD.get_dns_value(source_url=[redirect_domain + "." + self.domain])
        print(f"DNS Value: {domain_dns}")

        add_cname = self.AliDNS.cname_dns(name=redirect_domain, value=domain_dns)
        print(add_cname)



if __name__ == '__main__':
    master_domain = "soliton.wang"
    uuid = "uuid"
    token = "token"
    Ali_key = "keyid"
    Ali_Secret = "secret"
    sub = "test"
    redirect = "alog"
    port = "8899"
    app = Domain(
        domain=master_domain, mf_token=token, mf_uuid=uuid, ali_key=Ali_key, ali_secret=Ali_Secret
    )

    app.add_domain_and_mfpad_dns(sub_domain=sub, redirect_domain=redirect, port=port)


